package com.bsy.videostation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.bsy.framework.ext.onClick
import com.bsy.framework.manager.ActivityManager
import com.bsy.framework.utils.dpToPx
import com.bsy.videostation.adapter.lisener.OnItemClickListener
import com.bsy.videostation.model.VideoEntity
import com.bsy.videostation.model.VideoListEntity
import com.bsy.videostation.view_type.VideoEntityType
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dg.videostation.R
import me.jessyan.autosize.AutoSize

/**
 * Created by since on 2024/5/28.
 * Copyright
 * 描述：
 */
class VideoListAdapter : BaseMultiItemQuickAdapter<VideoListEntity, BaseViewHolder>() {
    init {
        addItemType(VideoEntityType.PORTRAIT.value, R.layout.item_video_list_horizontal)
        addItemType(VideoEntityType.LANDSCAPE.value, R.layout.item_video_list_vertical)
    }

    private var listener: OnItemClickListener<VideoEntity>? = null
    fun setCustomOnItemClickListener(listener: OnItemClickListener<VideoEntity>) {
        this.listener = listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        AutoSize.autoConvertDensity(ActivityManager.top(), 1366F, true)
        return super.onCreateViewHolder(parent, viewType)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val emptyView = LayoutInflater.from(context).inflate(R.layout.list_empty, recyclerView, false)
        setEmptyView(emptyView)
    }

    override fun convert(holder: BaseViewHolder, item: VideoListEntity) {

        when (holder.itemViewType) {
            VideoEntityType.PORTRAIT.value -> {
                item.hItem.forEachIndexed { index, videoEntity ->
                    when (index) {
                        0 -> bindItem(holder, videoEntity, R.id.iv1, R.id.iv1_app, R.id.tv1_title)
                        1 -> bindItem(holder, videoEntity, R.id.iv2, R.id.iv2_app, R.id.tv2_title)
                        2 -> bindItem(holder, videoEntity, R.id.iv3, R.id.iv3_app, R.id.tv3_title)
                        3 -> bindItem(holder, videoEntity, R.id.iv4, R.id.iv4_app, R.id.tv4_title)
                        4 -> bindItem(holder, videoEntity, R.id.iv5, R.id.iv5_app, R.id.tv5_title)
                    }
                }
            }

            VideoEntityType.LANDSCAPE.value -> {
                item.vItem.forEachIndexed { index, videoEntity ->
                    when (index) {
                        0 -> bindItem(holder, videoEntity, R.id.iv1, R.id.iv1_app, R.id.tv1_title)
                        1 -> bindItem(holder, videoEntity, R.id.iv2, R.id.iv2_app, R.id.tv2_title)
                        2 -> bindItem(holder, videoEntity, R.id.iv3, R.id.iv3_app, R.id.tv3_title)
                    }
                }
            }
        }
    }

    private fun bindItem(
        holder: BaseViewHolder, videoEntity: VideoEntity, imageViewId: Int, appIconId: Int, titleId: Int
    ) {
        holder.getView<ImageView>(imageViewId).load(videoEntity.cover) {
            error(R.drawable.no_img)
            transformations(RoundedCornersTransformation(dpToPx(10F)))
        }
        holder.getView<ImageView>(appIconId).load(videoEntity.sourceIcon) {
            transformations(RoundedCornersTransformation(dpToPx(10F)))
        }
        holder.setText(titleId, videoEntity.title)
        holder.getView<ImageView>(imageViewId).onClick(500) {
            listener?.onClick(videoEntity.index, videoEntity)
        }
    }
}